package com.wxmp.wxapi.util;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * @program: demo-test
 * @description: 假期工具类
 */
public class HolidayUtil {

    /**
     * 获取假期时间
     *
     * @param holidayName 假期名称
     * @param year        当前年份
     * @return
     */
    public static Date getHoliday(String holidayName, int year) {
        Calendar now = Calendar.getInstance();
        switch (holidayName) {
            case HolidayConstant.NEWYEARDAY:
                DateTime newYearDay = DateUtil.parse(year + "-1-1");
                Calendar newYearDayCalendar = newYearDay.toCalendar();
                if (now.after(newYearDayCalendar)) {
                    return DateUtil.parse(year + 1 + "-1-1");
                } else {
                    return newYearDay;
                }
            case HolidayConstant.CHINESENEWYEARGREGORIANFESTIVAL:
                ChineseDate chineseNewYearFestival = new ChineseDate(year, 1, 1);
                Calendar gregorianCalendar = chineseNewYearFestival.getGregorianCalendar();
                if (now.after(gregorianCalendar)) {
                    return new ChineseDate(year + 1, 1, 1).getGregorianDate();
                } else {
                    return chineseNewYearFestival.getGregorianDate();
                }

            case HolidayConstant.QINGMINGFESTIVAL:
                int param = year - 2000;
                int qingMingDay = (int) (param * 0.2422 + 4.81) - param / 4;
                DateTime qingMingDateTime = DateUtil.parse(year + "-4-" + qingMingDay);
                if (now.after(qingMingDateTime.toCalendar())) {
                    return DateUtil.parse(year + 1 + "-4-" + qingMingDay);
                } else {
                    return qingMingDateTime;
                }

            case HolidayConstant.LABORDAY:
                DateTime laborDayLaborDay = DateUtil.parse(year + "-5-1");
                if (now.after(laborDayLaborDay.toCalendar())) {
                    return DateUtil.parse(year + 1 + "-5-1");
                } else {
                    return laborDayLaborDay;
                }

            case HolidayConstant.DRAGONBOATGREGORIANFESTIVAL:
                ChineseDate dragonBoatFestival = new ChineseDate(year, 5, 5);
                Calendar dragonBoatFestivalCalendar = dragonBoatFestival.getGregorianCalendar();
                if (now.after(dragonBoatFestivalCalendar)) {
                    return new ChineseDate(year + 1, 5, 5).getGregorianDate();
                } else {
                    return dragonBoatFestival.getGregorianDate();
                }

            case HolidayConstant.MIDAUTUMNGREGORIANFESTIVAL:
                ChineseDate midAutumnFestival = new ChineseDate(year, 8, 15);
                Calendar midAutumnFestivalCalendar = midAutumnFestival.getGregorianCalendar();
                if (now.after(midAutumnFestivalCalendar)) {
                    return new ChineseDate(year + 1, 8, 15).getGregorianDate();
                } else {
                    return midAutumnFestival.getGregorianDate();
                }

            case HolidayConstant.NATIONALDAY:
                DateTime nationalDay = DateUtil.parse(year + "-10-1");
                Calendar nationalDayCalendar = nationalDay.toCalendar();
                if (now.after(nationalDayCalendar)) {
                    return DateUtil.parse(year + 1 + "-10-1");
                } else {
                    return nationalDay;
                }

            case HolidayConstant.VALENTINE_DAY_2_14:
                DateTime valentineDay_2_14 = DateUtil.parse(year + "-2-14");
                Calendar valentineDay_2_14Calendar = valentineDay_2_14.toCalendar();
                if (now.after(valentineDay_2_14Calendar)) {
                    return DateUtil.parse(year + 1 + "-2-14");
                } else {
                    return valentineDay_2_14;
                }

            case HolidayConstant.VALENTINE_DAY_7_7:
                ChineseDate valentineDay_7_7 = new ChineseDate(year, 7, 7);
                Calendar valentineDay_7_7Calendar = valentineDay_7_7.getGregorianCalendar();
                if (now.after(valentineDay_7_7Calendar)) {
                    return new ChineseDate(year + 1, 7, 7).getGregorianDate();
                } else {
                    return valentineDay_7_7.getGregorianDate();
                }
            default:
                return new Date();
        }
    }


}
